package com.seeseafish.mongo.dao.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created by chencheng on 2016/8/12.
 */
public class JDBCOperation {

    public static final String CONFIG_FILE_PATH = "/mongodb.properties";
    private static Properties props;

    public static Connection getConn(){
        if (props == null) {
            props = new Properties();
            InputStream is = null;
            try {
                is = JDBCOperation.class.getResourceAsStream(CONFIG_FILE_PATH);
                props.load(is);
            } catch (Exception e) {
               throw new RuntimeException("读取系统配置文件时发生错误", e);
            } finally {
                if(is != null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        String url = props.getProperty("jdbc.url");
        String username = props.getProperty("jdbc.username");
        String password = props.getProperty("jdbc.password");
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}
